//
//  WKBlockChainManager.m
//  ourToken
//
//  Created by wakeup on 2018/6/21.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKBlockChainManager.h"

#include "nmw_wallet_core.hpp"
using namespace nmw_wallet;

@implementation WKBlockChainManager

//刷新助记词
+ (NSString *)refreshMnemonic {
    std::string mnemonic = NmwWalletCore::getMnemonic();
    NSString *mnemonicMsg = [NSString stringWithCString:mnemonic.c_str() encoding:[NSString defaultCStringEncoding]];
    return mnemonicMsg;
}

//获取私钥和地址
+ (NSArray *)getPrivateAndAddressFormMnemonic:(NSString *)mnemonic type:(int)type {
    
    //比特币测试网络是 m/44'/1'、比特币是 m/44'/0'、以太坊是 m/44'/60'
    std::string tempMnemonic = std::string([mnemonic UTF8String]);
    std::string addressPrivateKey;
    if (type == 1) {
        addressPrivateKey = NmwWalletCore::derivedPrivateByMnemonic(tempMnemonic,"123456","m / 44' / 1' / 0' / 0 / 0");
    }
    else if (type == 2) {
        addressPrivateKey = NmwWalletCore::derivedPrivateByMnemonic(tempMnemonic,"123456","m / 44' / 0' / 0' / 0 / 0");
    }
    else if (type == 3) {
        addressPrivateKey = NmwWalletCore::derivedPrivateByMnemonic(tempMnemonic,"123456","m / 44' / 60' / 0' / 0 / 0");
    }

    NSString *addressPrivateKeyMsg = [NSString stringWithCString:addressPrivateKey.c_str() encoding:[NSString defaultCStringEncoding]];
    NSArray* addressPrivateKeyArray = [addressPrivateKeyMsg componentsSeparatedByString: @"\n"];
    
    //以太坊地址前面加0x
    if (type == 3) {
        NSString *address1 = addressPrivateKeyArray[0];
        NSString *address2 = [NSString stringWithFormat:@"0x%@",address1];
        NSString *private2 = addressPrivateKeyArray[1];
        NSArray *addressPrivateKeyArray1 = @[address2,private2];

        return addressPrivateKeyArray1;
    }

    return addressPrivateKeyArray;
}

//获取地址
+ (NSString *)addressFormPrivate:(NSString *)Private {
    std::string tempPrivateKey = std::string([Private UTF8String]);
    std::string address = NmwWalletCore::derivedPrivateByKey(0,tempPrivateKey);
    NSString *addressMsg = [NSString stringWithCString:address.c_str() encoding:[NSString defaultCStringEncoding]];
    return addressMsg;
}

//生成比特币交易
+ (NSString *)buildBtcTransactionWithPrivate:(NSString *)Private dict:(NSDictionary *)dict {
    
    NSString *networking = @"testnet";//测试网络:testnet 上架网络:mainnet
    NSString *tempDict = [self jsonStringWithDictionary:dict];
    const char * params1 = [networking UTF8String];
    const char * params2 = [Private UTF8String];
    const char * params3 = [tempDict UTF8String];
    std::string data = NmwWalletCore::buildBtcRawTransaction(params1,params2,params3);
    
    
//    std::string data = NmwWalletCore::buildBtcRawTransaction("testnet",
//                                                             "xprv9uBFXrfde1nkP91P7ALdZdRgn6vWuXEMGmC2vsWiYax88vP6nhvX1s5hV8oFx2gitBTLqrvccd5TqqKxdoD5n7jzJUuQvQYsDwMRjvu7C9Z",
//                                                             "{\"input\":[{\"txHash\": \"d2d33cb18debd1734aaa83be440b0f2e259189a5f39718338009f5ee38bf09cc\",\"txIndex\": 0},{\"txHash\": \"9d96db55fc3fae78f01cc35f2062fa5b24c18c461c272dc27af58bbf51429900\",\"txIndex\": 1}],\"output\":[{\"toAddress\": \"mxXwFP4LEjRTtW2Busuxux8gmtsjJSTAAg\",\"value\": 10000000},{\"toAddress\":\"muh4t1FQC5onS9u7xfCXB7p9WfzAYKYCv1\",\"value\": 107960000}]}");
    
    NSString *dataStr = [NSString stringWithCString:data.c_str()
                                           encoding:[NSString defaultCStringEncoding]];
    return dataStr;
}

//生成以太坊交易
+ (NSString *)buildEthTransactionWithPrivate:(NSString *)Private dict:(NSDictionary *)dict {
    
    NSString *tempDict = [self jsonStringWithDictionary:dict];
    const char * params1 = [Private UTF8String];
    const char * params2 = [tempDict UTF8String];
    std::string data = NmwWalletCore::buildEthRawTransaction(params1,params2);
    
//    std::string data = NmwWalletCore::buildEthRawTransaction("7c9eff472d72888a1c0aad763356e5d85c95c522a2c4a17dfd82b69f73276948",
//                                                             "{\"to\":\"0x319328a5f8a59ef835098d82de5e4e340db2c0b6\",\"value\":\"1\",\"nonce\":\"9\",\"gasPrice\":\"21000\",\"gasLimit\":\"42000\"}");
    
    NSString *dataStr = [NSString stringWithCString:data.c_str()
                                           encoding:[NSString defaultCStringEncoding]];
    return dataStr;
}

//字典转json字符串方法
+ (NSString *)jsonStringWithDictionary:(NSDictionary *)dict {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    
    if (!jsonData) {
//        NSLog(@"%@",error);
    }
    else {
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
}

@end
